/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.common;

import java.util.HashMap;
import java.util.Map;

import com.aeky.client.mvc.AppEvent;
import com.aeky.client.mvc.Dispatcher;
import com.aeky.client.rpcservice.GreetingRPCService;
import com.aeky.client.rpcservice.LoginRPCService;
import com.aeky.client.rpcservice.PrepareDataRPCService;
import com.aeky.client.rpcservice.UserRPCService;
import com.aeky.client.ui.AekyProgressBar;
import com.aeky.client.view.base.AekyWidget;
import com.aeky.client.view.base.ContentApplication;
import com.aeky.client.view.login.Login;
import com.aeky.client.view.login.LoginApplication;
import com.aeky.client.view.main.MainApplication;
import com.aeky.client.view.main.MainMenuController;
import com.aeky.client.view.main.MainPortal;
import com.aeky.client.view.welcome.CenterBodyPanelController;
import com.aeky.client.view.welcome.CenterHeadPanelController;
import com.aeky.client.view.welcome.MainPanel;
import com.aeky.client.view.welcome.WestPanelController;
import com.aeky.model.User;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.Window;

/**
 * 
 * @author aeky
 */
public class AppParams {

    private static AppParams instance = null;

    /** The application constants. */
    private ContentApplication app = null;

    /** The token widget. */
    private Map<String, AekyWidget> tokenWidget = new HashMap<String, AekyWidget>();

    /** 滚动条 */
    private com.smartgwt.client.widgets.Window progressBar;

    /** 用户信息 */
    private User user;

    private Dispatcher dispatcher;

    private AppParams() {

    }

    public static AppParams getInstance() {
        if (instance == null) {
            instance = new AppParams();
        }
        return instance;
    }

    public void showLogin() {
        Window.setTitle("用户登录");
        if (AppParams.getInstance().getApp() != null) {
            Window.Location.reload();
        }
        final LoginApplication application = new LoginApplication();
        AppParams.getInstance().setApp(application);
        AppParams.getInstance().getApp().setContent(new Login());
        AppParams.getInstance().getApp().show();
    }

    public void showMain() {
        // DataUtils.prepareBasicData();
        this.initBasicWidget();
        Window.setTitle("主页面");
        if (AppParams.getInstance().getApp() != null && !(AppParams.getInstance().getApp() instanceof MainApplication)) {
            AppParams.getInstance().getApp().clear();
        }
        MainApplication application = new MainApplication();
        AppParams.getInstance().setApp(application);
        MainPanel welcomePanel = new MainPanel();
        AppParams.getInstance().getApp().setContent(welcomePanel);
        AppParams.getInstance().getApp().show();
        this.addDispatcherController();
        this.forwardToView();
    }

    private void initBasicWidget() {
        Registry.registry(DemoConstants.CACHE_MAIN_PORTAL, new MainPortal());
    }

    private void addDispatcherController() {
        dispatcher = Dispatcher.get();
        dispatcher.getControllers().clear();
        dispatcher.addController(new MainMenuController());
        dispatcher.addController(new WestPanelController());
        dispatcher.addController(new CenterHeadPanelController());
        dispatcher.addController(new CenterBodyPanelController());
    }

    private void forwardToView() {
        AppEvent appEvent = new AppEvent(DemoEvents.INIT, null);
        Dispatcher.forwardEvent(appEvent);
    }

    /**
     * 获得rpc service
     * 
     * @param <T>
     * @param classType
     * @return
     */
    public static <T> T getService(Class classType) {
        T service = Registry.<T> get(classType.getName());
        if (service == null) {
            if (GreetingRPCService.class.getName().equals(classType.getName())) {
                service = GWT.create(GreetingRPCService.class);
            } else if (LoginRPCService.class.getName().equals(classType.getName())) {
                service = GWT.create(LoginRPCService.class);
            } else if (PrepareDataRPCService.class.getName().equals(classType.getName())) {
                service = GWT.create(PrepareDataRPCService.class);
            } else if (UserRPCService.class.getName().equals(classType.getName())) {
                service = GWT.create(UserRPCService.class);
            }
            Registry.registry(classType.getName(), service);
        }
        return service;
    }

    /**
     * Adds the widget history.
     * 
     * @param widget the widget
     */
    public void addWidgetHistory(AekyWidget widget) {
        if (widget == null) {
            return;
        }
        tokenWidget.put(widget.getToken(), widget);
    }

    /**
     * Find token widget.
     * 
     * @param token the token
     * 
     * @return the base widget
     */
    public AekyWidget findTokenWidget(String token) {
        return tokenWidget.get(token);
    }

    /**
     * Removes the token widget.
     * 
     * @param token the token
     */
    public void removeTokenWidget(String token) {
        this.tokenWidget.remove(token);

    }

    /**
     * Gets the current widget.
     * 
     * @return the current widget
     */
    public AekyWidget getCurrentWidget() {
        return this.getApp().getCurrentContent();
    }

    public void showProgressBar() {
        if (progressBar != null && progressBar.isVisible()) {
            return;
        }
        progressBar = new AekyProgressBar();
        progressBar.show();
    }

    public void hideProgressBar() {
        if (progressBar != null) {
            progressBar.hide();
            progressBar = null;
        }
    }

    /**
     * @return the app
     */
    public ContentApplication getApp() {
        return app;
    }

    /**
     * @param app the app to set
     */
    public void setApp(ContentApplication app) {
        this.app = app;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}
